package org.gwt.pluginframework.plugin;

import java.util.ArrayList;
import java.util.List;

import org.gwt.pluginframework.common.IPlugin;
import org.gwt.pluginframework.common.Plugin;
import org.gwt.pluginframework.common.PluginManager;
import org.gwt.pluginframework.common.event.IPluginEvent;
import org.gwt.pluginframework.common.event.PluginEventHandler;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

public class PluginMonitor extends Plugin {
	
	private static final long serialVersionUID = 8882443111761800366L;

	protected Window window;
	
	protected Grid<PluginModelData> grid;

	@Override
	public void onInitialize() {
		registerEventHandler(new PluginEventHandler(CustomEvents.SHOW_PLUGIN_MONITOR_EVENT) {
			public void handle(IPluginEvent event) {
				if(window == null) {
					start();
				}
				window.show();
			}
		});
	}
	
	@Override
	public void onStart() {
		window = new Window();
		window.setLayout(new FitLayout());
		window.setWidth(380);
		window.setHeight(300);
		window.setHeading("Plugin monitor");
		window.setScrollMode(Scroll.AUTOY);
		
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
		  
	    ColumnConfig column = new ColumnConfig();  
	    column.setId("name");  
	    column.setHeader("Name");  
	    column.setWidth(200);  
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("version");  
	    column.setHeader("Version");  
	    column.setWidth(50);  
	    configs.add(column);
	    
	    column = new ColumnConfig();  
	    column.setId("started");  
	    column.setHeader("Started");  
	    column.setWidth(75);  
	    configs.add(column);
	   
	    ColumnModel cm = new ColumnModel(configs); 
	    
	    ListStore<PluginModelData> store = new ListStore<PluginModelData>();
	    
	    grid = new Grid<PluginModelData>(store, cm);  
	    //grid.setStyleAttribute("borderTop", "none");  
	    //grid.setAutoExpandColumn("name");  
		
		window.add(grid);
		
		window.addButton(new Button("Refresh", new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				update();
			}
		}));
	
		update();
	}
	
	protected void update() {
		ListStore<PluginModelData> store = grid.getStore();
		store.removeAll();
		for(IPlugin plugin : ((PluginManager) manager).getPlugins()) {
	    	store.add(new PluginModelData(plugin));
	    }
		window.layout();
	}
	
	// FIXME generate from annotation
	public void setupManifest() {
		manifest.setName("PluginMonitor");
		manifest.setVersion("1.0");
	}

	class PluginModelData extends BaseModelData {

		private static final long serialVersionUID = 8677559897682842711L;
		
		protected IPlugin plugin;
		
		public PluginModelData(IPlugin plugin) {
			super();
			
			this.plugin = plugin;
//			set("name", plugin.getName());
//			set("version", plugin.getVersion());
//			set("started", plugin.isStarted());
		}
		
//		@SuppressWarnings("unchecked")
//		@Override
//		public <X> X get(String property) {
//			if("name".equals(property)) {
//				return (X) plugin.getName();
//			} else if("version".equals(property)) {
//				return (X) plugin.getVersion();
//			} else if("started".equals(property)) {
//				return (X) (Boolean) plugin.isStarted();
//			}
// 			return super.get(property);
//		}
		
		@SuppressWarnings("unchecked")
		public Object get(String property) {
			if("name".equals(property)) {
				return plugin.getName();
			} else if("version".equals(property)) {
				return plugin.getVersion();
			} else if("started".equals(property)) {
				return (Boolean) plugin.isStarted();
			}
 			return super.get(property);
		}
	}
}
